include ApplicationHelper

class SettingsController < ApplicationController

  load_and_authorize_resource

  # GET /settings
  # GET /settings.xml
  def index
    save_redirect_back request.request_uri
    
    @settings = Setting.search(params[:search_var], params[:page])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @settings }
    end
  end

  # GET /settings/1
  # GET /settings/1.xml
  def show
    @setting = Setting.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @setting }
    end
  end

  # GET /settings/new
  # GET /settings/new.xml
  def new
    @setting = Setting.new
    @type = Setting::VALID_TYPES[0]
    @format_value = get_format_value @type
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @setting }
    end
  end

  # GET /settings/1/edit
  def edit
    @setting = Setting.find(params[:id])

    if @setting.value.kind_of?(TrueClass) || @setting.value.kind_of?(FalseClass)
      @type = "Boolean"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(Integer)
      @type = "Integer"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(Float)
      @type = "Float"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(String)
      @type = "String"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(Text)
      @type = "Text"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(Time)
      @type = "Time"
      @str_value = @setting.value
    end

    if @setting.value.kind_of?(Date)
      @type = "Date"
      @str_value = @setting.value
    end

    @format_value = get_format_value @type
  end


  # POST /settings
  # POST /settings.xml
  def create 
    @setting = Setting.new
    @setting.var = params[:setting][:var]
    @type = params[:type]
    @str_value = params[:value]
    @format_value = get_format_value @type
    @setting.value = str_value_to_type_value(@type, @str_value)    

    respond_to do |format|
      if @setting.save
        flash[:notice] = t('msg_record_successfully_created')
        format.html { redirect_to(@setting) }
        format.xml  { render :xml => @setting, :status => :created, :location => @setting }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @setting.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /settings/1
  # PUT /settings/1.xml
  def update
    @setting = Setting.find(params[:id])
    @setting.var = params[:setting][:var]
    @type = params[:type]
    @str_value = params[:value]
    @format_value = get_format_value @type
    @setting.value = str_value_to_type_value(@type, @str_value)
    
    respond_to do |format|
      if @setting.save
        flash[:notice] = t('msg_record_successfully_updated')
        format.html { redirect_to(@setting) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @setting.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /settings/1
  # DELETE /settings/1.xml
  def destroy
    @setting = Setting.find(params[:id])
    @setting.destroy

    respond_to do |format|
      format.html { redirect_to(settings_url) }
      format.xml  { head :ok }
    end
  end


  def str_value_to_type_value(type, str_value)
    if str_value == "" || str_value.nil?
      #para que falle
      return str_value
    else
      if type == "Boolean"
        return [true, "true", 1, "1", "T", "t"].include?(str_value.class == String ? str_value.downcase : value)
      end
      if type == "Integer"
        return str_value.to_i
      end
      if type == "Float"
        return str_value.to_f
      end
      if type == "Time"
        return str_value.to_time
      end
      if type == "String"
        return str_value
      end
      if type == "Text"
        return str_value
      end
      if type == "Date"
        return str_value.to_date
      end
    end
  end

  def get_format_value type
      if type == "Boolean"
        return "true|false"
      end
      if type == "Integer"
        return "n*"
      end
      if type == "Float"
        return "n*,m*"
      end
      if type == "Time"
        return "aaaa-mm-dd hh:mm:ss [+|-]nnnn"
      end
      if type == "String"
        return ""
      end
      if type == "Text"
        return ""
      end
      if type == "Date"
        return "aaaa-mm-dd"
      end
  end

  def format_value
    @format_value = get_format_value params['type']
    render :partial=>'show_format_value' , :layout=>false
  end
  
  def redirect_back
      redirect_back_or_default params[:url]
  end
end